
#include "LocalDirIterator.h"
#include <QDir>
#include <QUrl>
#include <QDebug>

// Qt lib import
#include <QFileDialog>
#include <QStandardPaths>

LocalDirIterator::LocalDirIterator(QObject *parent, const QString &path)
    : QObject(parent), position(0)
{
    onChangeFolder(path);
}

bool LocalDirIterator::isImageFile(const QString &path)
{
    return (path.endsWith(".jpg", Qt::CaseInsensitive) || path.endsWith(".png", Qt::CaseInsensitive));
}

bool LocalDirIterator::empty() const
{
    return imageList.length() == 0;
}

void LocalDirIterator::nextImage()
{
    position++;
    if (position >= imageList.length())
    {
        position = 0;
    }
    qDebug() << "Next:" << currentFilename();
    emit currentFilenameChanged();
    
}

void LocalDirIterator::previousImage()
{
    if (position == 0)
    {
        if (imageList.length() > 0)
        {
            position = imageList.length() - 1;
        }
    }
    else
    {
        position--;
    }
    qDebug() << "previousImage:" << currentFilename();
    emit currentFilenameChanged();
}
//-------------------------------------------
bool LocalDirIterator::onChangeFolder(QString iPath)
{
#ifdef _WIN32
    // for windows folder,remove file:/// 
    if( iPath.startsWith("file:///"))
        iPath = iPath.mid(8); //start from 8
#elif __APPLE__
    // for mac folder,remove file://
    if( iPath.startsWith("file://"))
        iPath = iPath.mid(7); //start from 7

    else if( iPath.startsWith("qrc:"))
        iPath = iPath.mid(4); //start from 4
#endif

    imageList.clear();
    if (m_FolderPath != iPath)
    {
        m_FolderPath = iPath; // copy
        emit currentFolderChanged();
    }

    qDebug()<<iPath;
    qDebug()<<m_FolderPath;
#ifdef __APPLE__
    // for mac folder,after emit currentFolderChanged,m_FolderPath add  prefix qrc:
    if( m_FolderPath.startsWith("qrc:"))
        m_FolderPath = m_FolderPath.mid(4); //start from 4
#endif

    QFileInfo fileInfo(iPath);
    if (fileInfo.isFile())
    {
        // for file
        if (isImageFile(iPath))
        {
            imageList.append(fileInfo.absoluteFilePath());
        }
    }
    else
    { 
        QDir dir(iPath);
        dir.setFilter(QDir::Files);

        QStringList filename ;
        filename << "*.png" << "*.jpg";// add types
        QStringList results;
        results = dir.entryList(filename,QDir::Files | QDir::Readable,QDir::Name);

        //results are the files
        qDebug()<<"results are the files:";
        qDebug()<<results;

        for (const QString & name : results)
        {
            QFileInfo finfo(iPath,name);
            QString path2 = finfo.absoluteFilePath();
            imageList.append(path2);
            qDebug() << path2;
        }
    }

    emit currentFilenameChanged();
    return true;
}
//-------------------------------------------
QUrl LocalDirIterator::currentFilename() const
{
    if (empty())
        return QUrl("");
    return QUrl("file:" + imageList[position]);
}
//-------------------------------------------
QString LocalDirIterator::currentFolder() const
{
    return m_FolderPath;
}
//-------------------------------------------
void LocalDirIterator::onPreviousClicked()
{
    previousImage();
}

void LocalDirIterator::onNextClicked()
{
    nextImage();
}
